2025-05-29

Custom Emacs qalc REPL Pop-Up

I like to use qalc for calculating things. I know emacs has both calc and quick-calc, but I am very used to qalc, more specifically it's CLI version.

I mostly stick to qalc because it understands (custom) units. I am currently working with a binary fileformat which has multiple odd ways to count time. Qalc is very helpful here:

> 1.5625ns to 260.41666ps

  1.5625 nanoseconds ≈ 6.000000154 × 260.41666 ps

I could get that number without qalc, but it gets me there just a tiny bit more comfortably.

So for a long time now I've always just opened a terminal running qalc next to emacs. It only recently occured to me that I can run qalc inside emacs!

So first, here is a function which wraps any programm that presents a REPL in an emacs buffer plus window. We are using normal shell mode, so we only have to worry about wrangling buffers.

(defun lhp/replify (cmd bname)
  (if (string= bname (buffer-name))
    (quit-window)
  (pop-to-buffer
   (or (get-buffer bname)
       (window-buffer
        (async-shell-command cmd bname))))))

(defun qalc ()
  "Open a qalc REPL"
  (interactive)
  (lhp/replify "qalc" "*qalc*"))

A REPL function using this helper function will try to focus the window displaying the REPL buffer, optionally creating the buffer, launching the program and creating the window. Is the buffer already focused, instead close the window.

As you can probably tell, I really like dwim – "Do what I mean" – commands in emacs. Functions which can do one of multiple different actions, based on a usually reasonable, heuristics based guess.

Arguably closing the window is a more opinionated choice than hiding the buffer. But it makes sense if I show you the second half of this:

(setq display-buffer-alist
      '(;; ...

        ("\*qalc\*"
         (display-buffer-reuse-mode-window
          display-buffer-in-side-window)
         (dedicated . t)
         (inhibit-switch-frame . t)
         (side . bottom))

        ;; ...
        ))

Using an entry in display-buffer-alist, I told emacs to open my special *qalc* REPL buffer in a dedicated side-window at the bottom of the frame. Combined with a keybind for the qalc function (I use C-c q), I have a very quick and convenient way of showing and hiding a popup with a qalc REPL.

I literally have to think about nothing: Press C-c q once to open the REPL. Press again to hide it. My window layout remains undisturbed thanks to using a side-window and emacs takes care of spawning the program if it is not already running. And since the window is closed but the buffer isn't killed, the REPL session will stay alive; Basically like a little math scratchpad.

Side-windows are the best way I have found to make emacs window management pleasant. Everything that comes and goes often and isn't the main buffer required for a task should probably be a side-window. For me that mostly means my qalc REPL (bottom), compilation (bottom) and rgrep plus occur (right).

tub.webp

Warm air and buzzing insects surround me. The kind of day were you can actually work outside. The kind of work that feels good but isn't too productive. The kind of productivity that is more fun than ultimately useful.

Why bring the kitchen sink if you can bring a bath tub?

I decided to share this as a blog post because I think chances are high someone else will find this useful as well. I hope your day is as lovely as mine 🐈

Articles from blogs I read (generated by openring)

whippet lab notebook: guile, heuristics, and heap growth

Greets all! Another brief note today. I have gotten Guile working with one of the Nofl-based collectors, specifically the one that scans all edges conservatively (heap-conservative-mmc / heap-conservative-parallel-mmc). Hurrah!It was a pleasant surprise h…

wingolog, May 22, 2025

Status update, May 2025

Hi! Today wlroots 0.19.0 has finally been released! Among the newly supported protocols, color-management-v1 lays the first stone of HDR support (backend and renderer bits are still being reviewed) and ext-image-copy-capture-v1 enhances the previous screen ca…

emersion, May 15, 2025

Summary of changes for April 2025

Hey everyone!This is the list of all the changes we've done to our projects during the month of April. 100r.co, updated water, ditch bag, woodstove installation, and added new photos and information on first-aid kit. Rabbit Waves, updated Triangular…

Hundred Rabbits, April 30, 2025